package com.cloud.cloudvideo.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.stereotype.Service;

import com.cloud.cloudvideo.entity.SysUser;
import com.cloud.cloudvideo.entity.SysUserExample;
import com.cloud.cloudvideo.entity.SysUserExample.Criteria;
import com.cloud.cloudvideo.mapper.SysUserMapper;
import com.cloud.cloudvideo.service.UserService;
import com.cloud.cloudvideo.utils.Constants;
import com.cloud.cloudvideo.utils.MD5Utils;
import com.github.pagehelper.PageHelper;
import com.rabbitmq.client.AMQP.Exchange;

@Service
public class UserServiceImpl implements UserService {

	
	@Autowired
	private SysUserMapper userMapper;
	
	@Override
	public List<SysUser> list(SysUser user, Integer page) {
		SysUserExample example = new SysUserExample();
		Criteria criteria = example.createCriteria();
		if(null != user.getType()) {
			criteria.andTypeEqualTo(user.getType());
		}
		PageHelper.startPage(page,15);
		List<SysUser> list = userMapper.selectByExample(example);
		return list;
	}

	@Override
	public void add(SysUser user) {
		if(null == user.getId()) {
			user.setCreateBy(1L);
			user.setCreateTime(new Date());
			user.setType(Constants.USER_TYPE_ADMIN);
			if(null == user.getPassword())
				user.setPassword(MD5Utils.stringToMD5("123456"));
			userMapper.insertSelective(user);
		}else {
			user.setUpdateTime(new Date());
			userMapper.updateByPrimaryKeySelective(user);
		}
		
	}

	@Override
	public SysUser userInfo(Integer id) {
		return userMapper.selectByPrimaryKey(id.longValue());
	}

}
